'''
Created on Apr 16, 2012

@author: lavang
'''
from selenium import webdriver
from Interface.administratorPage import xpathUsers
from Interface.usersPage import xpathNewUser, xpathCbbAvatar, xpathTxtLogin,\
    xpathTxtPassword, xpathTxtRePassword, xpathTxtFirstName, xpathTxtLastName,\
    xpathCbbUserType, xpathCbbLanguage, xpathCbbTimeZone, xpathDescription,\
    xpathTxtAreaComment, xpathChkActive, xpathLinkUser, xpathBtnSubmit,\
    xpathUser, xpathDeleteUser, xpathTxtSearchUser
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
 
def clickOnUsers(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathUsers())
    element.click()
    
def clickOnNewUser(self):
    #browser=webdriver.Firefox()
    browser=self.browser
    element=browser.find_element_by_xpath(xpathNewUser())
    element.click()
    
def createUser(self,avatar,login,password,rePassword,firstName,lastName,email,userType,language,timeZone,description,comment,status='off',edit='off'):
    #browser=webdriver.Firefox()
    browser=self.browser
    
    # select avatar
    element=browser.find_element_by_xpath(xpathCbbAvatar())
    Select(element).select_by_visible_text(avatar)
    
    
    # enter login name. If edit='on', don't enter login name
    if edit=='off':
        element=browser.find_element_by_xpath(xpathTxtLogin())
        element.clear()
        element.send_keys(login)
    else:
        pass
    
    # enter password
    element=browser.find_element_by_xpath(xpathTxtPassword())
    element.clear()
    element.send_keys(password)
    
    # retype password
    element=browser.find_element_by_xpath(xpathTxtRePassword())
    element.clear()
    element.send_keys(rePassword)
    
    # enter firs name
    element=browser.find_element_by_xpath(xpathTxtFirstName())
    element.clear()
    element.send_keys(firstName)
    
    # enter last name
    element=browser.find_element_by_xpath(xpathTxtLastName())
    element.clear()
    element.send_keys(lastName)
    
    # select UserType
    element=browser.find_element_by_xpath(xpathCbbUserType())
    Select(element).select_by_visible_text(userType)
    
    # select language
    element=browser.find_element_by_xpath(xpathCbbLanguage())
    Select(element).select_by_visible_text(language)
    
    # select timezone
    element=browser.find_element_by_xpath(xpathCbbTimeZone())
    Select(element).select_by_visible_text(timeZone)
    '''
    # enter description
    element=browser.find_element_by_xpath(xpathDescription())
    element.clear()
    element.send_keys(description)
    '''
    # enter comment
    element=browser.find_element_by_xpath(xpathTxtAreaComment())
    element.clear()
    element.send_keys(comment)
    
    # check Active status
    element=browser.find_element_by_xpath(xpathChkActive())
    if status=='on':
        if element.is_selected():
            pass
        else: element.click()
    else:
        if element.is_selected():
            element.click()
            
    # click Submit
    element=browser.find_element_by_xpath(xpathBtnSubmit())
    element.click()
          
# back to User page
def clickOnLinkUser(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathLinkUser())
    element.click()
        
# verify a user is created successfully
def isUserCreatedSuccessfully(self,userName):
    browser=self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//span[contains(text(),'"+userName+"')]"))
        return True
    except:
        return False
    
# search User
def searchUser(self,userName):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathTxtSearchUser())
    element.send_keys(userName)
    element.send_keys(Keys.RETURN)

# delete an user    
def deleteUser(self,userName):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathDeleteUser(userName))
    element.click() 
    alert=browser.switch_to_alert()  
    alert.accept()     
    browser.refresh()
    browser.implicitly_wait(20)
    
# click on a user ( to change information)
def clickOnUserName(self,userName):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathUser(userName))
    element.click()
    
# Search 
def isUserSearch(self,userName):
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//span[contains(text(),'"+userName+"')]"))
        return True
    except:
        return False